﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyTrader.Option
{
    using CodeList = List<EasyTrader.DataSet.CodeTable>;
    using DDECollectionList = List<EasyTrader.Option.DDEData>;
    using SimpleDataList = List<EasyTrader.DataSet.SimpleData>;

    public partial class DDETable : System.Windows.Forms.Form
    {

        public class CellDoubleClickEvent : SourceGrid.Cells.Controllers.ControllerBase
        {
            private EasyTrader.Option.DDETable m_DDEFrm = null;
            public EasyTrader.Option.DDETable DDEFrm
            {
                get { return m_DDEFrm; }
                set { m_DDEFrm = value; }
            }

            private SourceGrid.Grid m_GridDDE = null;
            public SourceGrid.Grid GridDDE
            {
                get { return m_GridDDE; }
                set { m_GridDDE = value; }
            }

            public override void OnDoubleClick(SourceGrid.CellContext sender, EventArgs e)
            {
                base.OnDoubleClick(sender, e);
                SourceGrid.Cells.Cell curCell = (SourceGrid.Cells.Cell)sender.Cell;
                //MessageBox.Show(sender.Grid, curCell.Column.Index.ToString() + curCell.Row.Index.ToString());
                if (m_DDEFrm.TargetValueFilled == true)
                {
                    //EasyTrader.Chart.CallPutChart callPutChart = new EasyTrader.Chart.CallPutChart(m_DDEFrm.OpDataSet);
                    //callPutChart.Show();
                }
                SourceGrid.Grid gridPrice = (SourceGrid.Grid)sender.Grid;
                //string closeVal = gridPrice[m_OrderFrm.centerRow, 3].Value.ToString();
                //if (closeVal == "")
                //    return;


            }
        }


        private EasyTrader.Option.DDEDayData m_CurDayCol = null;
        public EasyTrader.Option.DDEDayData CurDayCol
        {
            get { return m_CurDayCol; }
            set { m_CurDayCol = value; }
        }
        private int m_WorkDate = 0;
        public int WorkDate
        {
            get { return m_WorkDate; }
            set { m_WorkDate = value; }
        }

        delegate void TimerEventFiredDelegate();

        //private TimerEventFiredDelegate DGTimer;
        //IAsyncResult ARTimer;

        private System.Timers.Timer timer = new System.Timers.Timer();

        private void InitTimer()
        {
            timer.Interval = 100;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimerTick);

            //DGTimer = new TimerEventFiredDelegate(OnTimerWork);
        }

        private void OnTimerTick(object sender, EventArgs e)
        {
            //ARTimer = BeginInvoke(DGTimer);
        }

        private bool m_TargetValueFilled = false;
        public bool TargetValueFilled
        {
            get { return m_TargetValueFilled; }
            set { m_TargetValueFilled = value; }
        }
        private void OnTimerWork()
        {
            if (m_TargetValueFilled == false )
            {
                //EasyTrader.Option.DDEDayDataCollection curDayCol = m_OpDataSet.FindDDEDayDataCol(m_OpDataSet.DownLoader.CurDay);
                //m_OpDataSet.DownLoader.FillTargetVal(curDayCol);
                //SetCurDayState(curDayCol);
                //SetPreDayState(curDayCol);
                //m_TargetValueFilled = true;
            }
        }

        private void ClearCrossVal(EasyTrader.Option.DDEDayData curDayCol)
        {
            if (curDayCol == null)
                return;

            DDECollectionList dataList = curDayCol.CollectionList;
            for (int i = 0; i < dataList.Count; i++)
            {
                EasyTrader.Option.DDEData curRowData = dataList[i];
                //Console.WriteLine(curRowData.PriceCode);
                EasyTrader.Option.DDEDataItem curItem = curRowData.DataItem;
                int rowID = FindRowID(curRowData.CallCode);
                gridDDE[rowID, 5].Value = "";
                gridDDE[rowID, 5].View = normalView;
                gridDDE[rowID + 2, 5].Value = "";
                gridDDE[rowID + 2, 5].View = normalView;
                gridDDE[rowID, 7].Value = "";
                gridDDE[rowID, 7].View = normalView;
                gridDDE[rowID + 2, 7].Value = "";
                gridDDE[rowID + 2, 7].View = normalView;
            }
        }

        private void ClearATM(EasyTrader.Option.DDEDayData curDayCol)
        {
            if (curDayCol == null)
                return;

            DDECollectionList dataList = curDayCol.CollectionList;
            for (int i = 0; i < dataList.Count; i++)
            {
                EasyTrader.Option.DDEData curRowData = dataList[i];
                //Console.WriteLine(curRowData.PriceCode);
                EasyTrader.Option.DDEDataItem curItem = curRowData.DataItem;
                int rowID = FindRowID(curRowData.CallCode);
                gridDDE[rowID, 6].View = centerView;
            }
        }

        private void SetCrossVal(int a_Date)
        {
            if (m_OpDataSet == null)
                return;

            EasyTrader.DataSet.DayCrossData curDayData = m_OpDataSet.FindOptCrossData(a_Date);
            if (curDayData == null)
                return;

            SimpleDataList  dataList = curDayData.DataList;
            for (int i = 0; i < dataList.Count; i++)
            {
                EasyTrader.DataSet.SimpleData curData = dataList[i];
                if (a_Date == curData.Date)
                {
                    string code = curData.CallCode;
                    int type = curData.Type;
                    string sideCode = code.Substring(0, 1);
                    double curVal = curData.Val;
                    int rowID = FindRowID(code);
                    {
                        // 마디가
                        if (curData.CallType == 0)
                        {
                            gridDDE[rowID + 2, 5].View = sellView;
                            gridDDE[rowID + 2, 5].Value = curData.CallVal;
                        }
                        else if (curData.CallType == 1) // 목표가
                        {
                            gridDDE[rowID, 5].View = buyView;
                            gridDDE[rowID, 5].Value = curData.CallVal;
                        }

                        // 마디가
                        if (curData.PutType == 0)
                        {
                            gridDDE[rowID + 2, 7].View = sellView;
                            gridDDE[rowID + 2, 7].Value = curData.PutVal;
                        }
                        else if (curData.PutType == 1) // 목표가
                        {
                            gridDDE[rowID, 7].View = buyView;
                            gridDDE[rowID, 7].Value = curData.PutVal;
                        }
                    }
                }
            }
        }

        private EasyTrader.ETDataSet m_OpDataSet = null;
        



        public DDETable()
        {
            InitializeComponent();
            InitGridCellView();
            InitGrid();
            InitTimer();
        }

        public DDETable(EasyTrader.ETDataSet opDataSet)
        {
            m_OpDataSet = opDataSet;
            InitializeComponent();
            InitGridCellView();
            InitGrid();
            InitTimer();
            m_OpDataSet.CreateDDEData();
            //m_OpDataSet.CreateMonthData();
            //m_OpDataSet.FillDDEMonthData();
            DDEDayData curDayCol = m_OpDataSet.FindDDEDayData(m_OpDataSet.DownLoader.CurDay);
            FillMonthData(curDayCol);
            SetATM();
            tbCurDay.Text = m_OpDataSet.DownLoader.CurDay.ToString();
            tbPreDay.Text = m_OpDataSet.DownLoader.PreDay.ToString();

            SetCrossVal(Convert.ToInt32(m_OpDataSet.DownLoader.CurDay));
        }

        private SourceGrid.Cells.Views.Cell categoryView = null;
        private SourceGrid.Cells.Views.Cell headerView = null;
        private SourceGrid.Cells.Views.Cell priceView = null;
        private SourceGrid.Cells.Views.Cell buyView = null;
        private SourceGrid.Cells.Views.Cell sellView = null;
        private SourceGrid.Cells.Views.Cell centerView = null;
        private SourceGrid.Cells.Views.Cell normalView = null;

        private SourceGrid.Cells.Views.Cell highView = null;
        private SourceGrid.Cells.Views.Cell lowView = null;
        private SourceGrid.Cells.Views.Cell baseView = null;
        private SourceGrid.Cells.Views.Cell curView = null;

        private SourceGrid.Cells.Views.Cell upView = null;
        private SourceGrid.Cells.Views.Cell downView = null;



        public void SetDay(object a_CurDay, object a_PreDay)
        {
            //tbCurDay.Text = a_CurDay.ToString();
            //tbPreDay.Text = a_PreDay.ToString();
        }


        public void InitGridCellView()
        {
            DevAge.Drawing.RectangleBorder border = new DevAge.Drawing.RectangleBorder(new DevAge.Drawing.BorderLine(Color.DarkGreen), new DevAge.Drawing.BorderLine(Color.DarkGreen));
            //Category header
            //DevAge.Drawing.BorderLine top = new DevAge.Drawing.BorderLine(Color.Blue, 2);
            //DevAge.Drawing.RectangleBorder rcBorder = new DevAge.Drawing.RectangleBorder();

            categoryView = new SourceGrid.Cells.Views.Cell();
            categoryView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.RoyalBlue, Color.LightBlue, 0);
            categoryView.ForeColor = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            categoryView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            categoryView.Border = DevAge.Drawing.RectangleBorder.NoBorder;
            categoryView.Font = new Font("굴림", 9, FontStyle.Bold);


            headerView = new SourceGrid.Cells.Views.Cell();
            headerView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.ForestGreen, Color.FloralWhite, 0);
            headerView.ForeColor = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            headerView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            headerView.Border = DevAge.Drawing.RectangleBorder.NoBorder;
            headerView.Font = new Font("굴림", 9, FontStyle.Bold);

            priceView = new SourceGrid.Cells.Views.Cell();
            priceView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.ForestGreen, Color.ForestGreen, 0);
            //priceView.ForeColor = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            priceView.ForeColor = Color.White;
            priceView.BackColor = Color.LightSteelBlue;
            priceView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            priceView.Border = DevAge.Drawing.RectangleBorder.RectangleBlack1Width;
            priceView.Font = new Font("굴림", 9, FontStyle.Regular);

            buyView = new SourceGrid.Cells.Views.Cell();
            buyView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.Red, Color.White, 0);
            //priceView.ForeColor = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            buyView.BackColor = Color.LightPink;
            buyView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            //priceView.Border = DevAge.Drawing.RectangleBorder.NoBorder;
            buyView.Font = new Font("굴림", 9, FontStyle.Regular);

            sellView = new SourceGrid.Cells.Views.Cell();
            sellView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.Blue, Color.White, 0);
            //priceView.ForeColor = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            sellView.BackColor = Color.LightSteelBlue;
            sellView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            //priceView.Border = DevAge.Drawing.RectangleBorder.NoBorder;
            sellView.Font = new Font("굴림", 9, FontStyle.Regular);

            centerView = new SourceGrid.Cells.Views.Cell();
            centerView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.FloralWhite, Color.LightSteelBlue, 90);
            centerView.ForeColor = Color.Red;
            centerView.BackColor = Color.LightSteelBlue;
            centerView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            //priceView.Border = DevAge.Drawing.RectangleBorder.NoBorder;
            centerView.Font = new Font("굴림", 9, FontStyle.Regular);

            normalView = new SourceGrid.Cells.Views.Cell();
            //normalView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.ForestGreen, Color.FloralWhite, 0);
            normalView.ForeColor = Color.Black;
            normalView.BackColor = Color.White;
            normalView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            normalView.Font = new Font("굴림", 9, FontStyle.Regular);


            highView = new SourceGrid.Cells.Views.Cell();
            highView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.LightPink, Color.White, 90);
            highView.ForeColor = Color.Black;
            highView.BackColor = Color.White;
            highView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            highView.Font = new Font("굴림", 9, FontStyle.Regular);

            lowView = new SourceGrid.Cells.Views.Cell();
            lowView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.LightBlue, Color.White, 90);
            lowView.ForeColor = Color.Black;
            lowView.BackColor = Color.White;
            lowView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            //lowView.Border. = DevAge.Drawing.BorderLine;
            lowView.Font = new Font("굴림", 9, FontStyle.Regular);


            baseView = new SourceGrid.Cells.Views.Cell();
            baseView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.LightGreen, Color.White, 90);
            baseView.ForeColor = Color.Red;
            baseView.BackColor = Color.White;
            baseView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            baseView.Font = new Font("굴림", 9, FontStyle.Bold);

            curView = new SourceGrid.Cells.Views.Cell();
            //curView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.White, Color.White, 90);
            curView.ForeColor = Color.Black;
            curView.BackColor = Color.White;
            curView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            curView.Font = new Font("굴림", 9, FontStyle.Regular);


            upView = new SourceGrid.Cells.Views.Cell();
            upView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.LightGreen, Color.White, 90);
            upView.ForeColor = Color.Red;
            upView.BackColor = Color.White;
            upView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            upView.Font = new Font("굴림", 9, FontStyle.Regular);

            downView = new SourceGrid.Cells.Views.Cell();
            downView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(Color.LightGreen, Color.White, 90);
            downView.ForeColor = Color.Blue;
            downView.BackColor = Color.White;
            downView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            downView.Font = new Font("굴림", 9, FontStyle.Regular);

        }

        private int FindRowID(object a_OpCode)
        {
            CodeList centerCodeList = m_OpDataSet.DownLoader.CenterPriceCodeList;
            string opCode = a_OpCode.ToString();
            int rowID = 0;
            for (int i = 0; i < centerCodeList.Count; i++)
            {
                EasyTrader.DataSet.CodeTable curCode = centerCodeList[i];
                if (curCode.CallCode == opCode || curCode.PutCode == opCode)
                {
                    rowID = curCode.CellRowNum;
                }
            }

            return rowID;
        }

        private void InitGrid()
        {
            if (m_OpDataSet == null)
                return;

            CellDoubleClickEvent doubleClickController = new CellDoubleClickEvent();
            doubleClickController.GridDDE = gridDDE;
            doubleClickController.DDEFrm = this;


            gridDDE.Redim(92, 13);
            gridDDE.FixedRows = 2;
            gridDDE.Rows[0].Height = 30;

            gridDDE[0, 0] = new SourceGrid.Cells.Cell("콜옵션");
            gridDDE[0, 0].SetSpan(1, 5);
            gridDDE[0, 0].View = buyView;
            gridDDE[1, 5] = new SourceGrid.Cells.Cell("목/교/마");
            gridDDE[1, 5].SetSpan(1, 1);
            gridDDE[1, 5].View = headerView;
            gridDDE[1, 5].Column.Width = 110;
            gridDDE[0, 5] = new SourceGrid.Cells.Cell("행사가격");
            gridDDE[0, 5].SetSpan(1, 3);
            gridDDE[0, 5].View = headerView;
            gridDDE[0, 5].Column.Width = 120;
            gridDDE[1, 7] = new SourceGrid.Cells.Cell("목/교/마");
            gridDDE[1, 7].SetSpan(1, 1);
            gridDDE[1, 7].View = headerView;
            gridDDE[1, 7].Column.Width = 110;

            gridDDE[0, 8] = new SourceGrid.Cells.Cell("풋옵션");
            gridDDE[0, 8].SetSpan(1, 5);
            gridDDE[0, 8].View = sellView;

            gridDDE[1, 0] = new SourceGrid.Cells.Cell("전일 상태");
            gridDDE[1, 0].View = buyView;
            gridDDE[1, 0].Column.Width = 100;
            gridDDE[1, 1] = new SourceGrid.Cells.Cell("현재 상태");
            gridDDE[1, 1].View = buyView;
            gridDDE[1, 1].Column.Width = 100;
            gridDDE[1, 2] = new SourceGrid.Cells.Cell("고/기/저");
            gridDDE[1, 2].View = buyView;
            gridDDE[1, 2].Column.Width = 110;
            gridDDE[1, 3] = new SourceGrid.Cells.Cell("전일");
            gridDDE[1, 3].View = buyView;
            gridDDE[1, 3].Column.Width = 110;
            gridDDE[1, 4] = new SourceGrid.Cells.Cell("당일");
            gridDDE[1, 4].View = buyView;
            gridDDE[1, 4].Column.Width = 110;
            gridDDE[1, 8] = new SourceGrid.Cells.Cell("당일");
            gridDDE[1, 8].View = sellView;
            gridDDE[1, 8].Column.Width = 110;
            gridDDE[1, 9] = new SourceGrid.Cells.Cell("전일");
            gridDDE[1, 9].View = sellView;
            gridDDE[1, 9].Column.Width = 110;
            gridDDE[1, 10] = new SourceGrid.Cells.Cell("고/기/저");
            gridDDE[1, 10].View = sellView;
            gridDDE[1, 10].Column.Width = 110;
            gridDDE[1, 11] = new SourceGrid.Cells.Cell("현재 상태");
            gridDDE[1, 11].View = sellView;
            gridDDE[1, 11].Column.Width = 100;
            gridDDE[1, 12] = new SourceGrid.Cells.Cell("전일 상태");
            gridDDE[1, 12].View = sellView;
            gridDDE[1, 12].Column.Width = 100;

            // Fill Price Range
            for (int i = 2; i < gridDDE.RowsCount; i++)
            {
                gridDDE[i, 5] = new SourceGrid.Cells.Cell();
                gridDDE[i, 7] = new SourceGrid.Cells.Cell();
                gridDDE[i, 5].View = curView;
                gridDDE[i, 5].Column.Width = 110;
                gridDDE[i, 7].View = curView;
                gridDDE[i, 7].Column.Width = 110;
            }

            for (int i = 2; i < gridDDE.RowsCount; i = i + 3)
            {
                gridDDE[i, 6] = new SourceGrid.Cells.Cell();
                gridDDE[i, 6].View = centerView;
                gridDDE[i, 6].SetSpan(3, 1);
                gridDDE[i, 6].Column.Width = 60;
            }

            // Fill call side
            for (int i = 2; i < gridDDE.RowsCount; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    gridDDE[i, j] = new SourceGrid.Cells.Cell();
                    int mod = i % 3;
                    if (mod == 2)
                        gridDDE[i, j].View = highView;
                    else if (mod == 1)
                        gridDDE[i, j].View = lowView;
                    else if (mod == 0)
                        gridDDE[i, j].View = baseView;
                    else
                        gridDDE[i, j].View = normalView;
                    if (j == 0 || j == 1)
                        gridDDE[i, j].Column.Width = 100;
                    else
                        gridDDE[i, j].Column.Width = 110;
                }
            }

            for (int i = 2; i < gridDDE.RowsCount; i++)
            {
                for (int j = 8; j < 13; j++)
                {
                    gridDDE[i, j] = new SourceGrid.Cells.Cell();
                    int mod = i % 3;
                    if (mod == 2)
                        gridDDE[i, j].View = highView;
                    else if (mod == 1)
                        gridDDE[i, j].View = lowView;
                    else if (mod == 0)
                        gridDDE[i, j].View = baseView;
                    else
                        gridDDE[i, j].View = normalView;
                    if (j == 11 || j == 12)
                        gridDDE[i, j].Column.Width = 100;
                    else
                        gridDDE[i, j].Column.Width = 110;
                }
            }

            CodeList centerCodeList = m_OpDataSet.DownLoader.CenterPriceCodeList;
            CodeList callCodeList = m_OpDataSet.DownLoader.CallPriceCodeList;
            CodeList putCodeList = m_OpDataSet.DownLoader.PutPriceCodeList;

            for (int i = 2, j = 0; i < gridDDE.RowsCount && j < m_OpDataSet.DownLoader.PriceCodeList.Count; i = i + 3, j++)
            {
                gridDDE[i, 6].Value = m_OpDataSet.DownLoader.PriceCodeList[m_OpDataSet.DownLoader.PriceCodeList.Count - j - 1];
                gridDDE[i, 6].AddController(doubleClickController);
                string callCode = callCodeList[callCodeList.Count - j - 1].Code;
                string putCode = putCodeList[putCodeList.Count - j - 1].Code;
                EasyTrader.DataSet.CodeTable newCode = new EasyTrader.DataSet.CodeTable();
                newCode.CallCode = callCode;
                newCode.CellRowNum = i;
                newCode.Code = callCode;
                centerCodeList.Add(newCode);
                newCode = new EasyTrader.DataSet.CodeTable();
                newCode.PutCode = putCode;
                newCode.CellRowNum = i;
                newCode.Code = putCode;
                centerCodeList.Add(newCode);
            }

            //m_OpDataSet.DownLoader.StartTimer();
        }

        public void SetBaseDayData(object a_OpCode, object a_LowVal)
        {
            int rowID = FindRowID(a_OpCode);
            string sideCode = a_OpCode.ToString().Substring(0, 1);
            if (sideCode == "2")
            {
                gridDDE[rowID + 1, 2].Value = a_LowVal;
            }
            else if (sideCode == "3")
            {
                gridDDE[rowID + 1, 10].Value = a_LowVal;
            }
        }

        public void SetMonthData(object a_OpCode, object a_HighVal, object a_LowVal)
        {
            int rowID = FindRowID(a_OpCode);
            string sideCode = a_OpCode.ToString().Substring(0, 1);
            if (sideCode == "2")
            {
                gridDDE[rowID, 2].Value = a_HighVal;
                gridDDE[rowID + 2, 2].Value = a_LowVal;
            }
            else if (sideCode == "3")
            {
                gridDDE[rowID, 10].Value = a_HighVal;
                gridDDE[rowID + 2, 10].Value = a_LowVal;
            }
        }

        public void SetTargetVal(object a_OpCode, object a_CallFirst, object a_CallSecond, object a_CallThird, object a_PutFirst, object a_PutSecond, object a_PutThird)
        {
            int rowID = FindRowID(a_OpCode);
            gridDDE[rowID + 2, 5].Value = a_CallFirst;
            gridDDE[rowID + 1, 5].Value = a_CallSecond;
            gridDDE[rowID, 5].Value = a_CallThird;
            gridDDE[rowID + 2, 7].Value = a_PutFirst;
            gridDDE[rowID + 1, 7].Value = a_PutSecond;
            gridDDE[rowID, 7].Value = a_PutThird;
        }

        public void SetTargetVal(object a_OpCode, object a_CallCur, object a_PutCur, object a_CallFirst, object a_CallSecond, object a_CallThird, object a_PutFirst, object a_PutSecond, object a_PutThird)
        {
            int rowID = FindRowID(a_OpCode);
            double curVal = Convert.ToDouble(a_CallCur.ToString());
            double tarVal = Convert.ToDouble(a_CallFirst.ToString());
            double delta = tarVal - curVal;
            string valStr = "";
            valStr = tarVal.ToString("F2");
            valStr += "(" + delta.ToString("F2") + ")";
            gridDDE[rowID + 2, 5].Value = valStr;
            if (Math.Abs(delta) <= GlobalVar.MinOpDelta)
            {
                if (delta > 0)
                    gridDDE[rowID + 2, 5].View = sellView;
                else if (delta < 0)
                    gridDDE[rowID + 2, 5].View = buyView;
                else
                    gridDDE[rowID + 2, 5].View = normalView;
            }

            tarVal = Convert.ToDouble(a_CallSecond.ToString());
            delta = tarVal - curVal;
            valStr = tarVal.ToString("F2");
            valStr += "(" + delta.ToString("F2") + ")";
            gridDDE[rowID + 1, 5].Value = valStr;
            if (Math.Abs(delta) <= GlobalVar.MinOpDelta)
            {
                if (delta > 0)
                    gridDDE[rowID + 1, 5].View = sellView;
                else if (delta < 0)
                    gridDDE[rowID + 1, 5].View = buyView;
                else
                    gridDDE[rowID + 1, 5].View = normalView;
            }

            tarVal = Convert.ToDouble(a_CallThird.ToString());
            delta = tarVal - curVal;
            valStr = tarVal.ToString("F2");
            valStr += "(" + delta.ToString("F2") + ")";
            gridDDE[rowID, 5].Value = valStr;
            if (Math.Abs(delta) <= GlobalVar.MinOpDelta)
            {
                if (delta > 0)
                    gridDDE[rowID, 5].View = sellView;
                else if (delta < 0)
                    gridDDE[rowID, 5].View = buyView;
                else
                    gridDDE[rowID, 5].View = normalView;
            }

            curVal = Convert.ToDouble(a_PutCur.ToString());
            tarVal = Convert.ToDouble(a_PutFirst.ToString());
            delta = tarVal - curVal;
            valStr = tarVal.ToString("F2");
            valStr += "(" + delta.ToString("F2") + ")";
            gridDDE[rowID + 2, 7].Value = valStr;
            if (Math.Abs(delta) <= GlobalVar.MinOpDelta)
            {
                if (delta > 0)
                    gridDDE[rowID + 2, 7].View = sellView;
                else if (delta < 0)
                    gridDDE[rowID + 2, 7].View = buyView;
                else
                    gridDDE[rowID + 2, 7].View = normalView;
            }

            tarVal = Convert.ToDouble(a_PutSecond.ToString());
            delta = tarVal - curVal;
            valStr = tarVal.ToString("F2");
            valStr += "(" + delta.ToString("F2") + ")";
            gridDDE[rowID + 1, 7].Value = valStr;
            if (Math.Abs(delta) <= GlobalVar.MinOpDelta)
            {
                if (delta > 0)
                    gridDDE[rowID + 1, 7].View = sellView;
                else if (delta < 0)
                    gridDDE[rowID + 1, 7].View = buyView;
                else
                    gridDDE[rowID + 1, 7].View = normalView;
            }

            tarVal = Convert.ToDouble(a_PutThird.ToString());
            delta = tarVal - curVal;
            valStr = tarVal.ToString("F2");
            valStr += "(" + delta.ToString("F2") + ")";
            gridDDE[rowID, 7].Value = valStr;
            if (Math.Abs(delta) <= GlobalVar.MinOpDelta)
            {
                if (delta > 0)
                    gridDDE[rowID, 7].View = sellView;
                else if (delta < 0)
                    gridDDE[rowID, 7].View = buyView;
                else
                    gridDDE[rowID, 7].View = normalView;
            }
        }

        public void SetDailyData(object a_OpCode, object a_CurVal, object a_Open, object a_High, object a_Low, object a_PreHigh, object a_PreLow, object a_PreClose, object a_PreOpen)
        {
            int rowID = FindRowID(a_OpCode);
            string sideCode = a_OpCode.ToString().Substring(0, 1);

            gridDDE[2, 5].Value = m_OpDataSet.DownLoader.OptTodayTryCount;
            gridDDE[2, 7].Value = m_OpDataSet.DownLoader.OptTodayTryCount;
            string curVal = "";
            string preVal = "";
            if (sideCode == "2")
            {
                //gridDDE[rowID + 1, 5].Value = a_CurVal;

                curVal = a_Open.ToString() + " : " + a_CurVal.ToString();
                preVal = a_PreOpen.ToString() + " : " + a_PreClose.ToString();
                gridDDE[rowID, 3].Value = a_PreHigh;
                gridDDE[rowID + 1, 3].Value = preVal;
                gridDDE[rowID + 2, 3].Value = a_PreLow;

                gridDDE[rowID, 4].Value = a_High;
                gridDDE[rowID + 1, 4].Value = curVal;
                gridDDE[rowID + 2, 4].Value = a_Low;
            }
            else if (sideCode == "3")
            {
                //gridDDE[rowID + 1, 7].Value = a_CurVal; 
                curVal = a_Open.ToString() + " : " + a_CurVal.ToString();
                preVal = a_PreOpen.ToString() + " : " + a_PreClose.ToString();
                gridDDE[rowID, 9].Value = a_PreHigh;
                gridDDE[rowID + 1, 9].Value = preVal;
                gridDDE[rowID + 2, 9].Value = a_PreLow;

                gridDDE[rowID, 8].Value = a_High;
                gridDDE[rowID + 1, 8].Value = curVal;
                gridDDE[rowID + 2, 8].Value = a_Low;
            }
        }

        public void SetTodayData(object a_OpCode, object a_CurVal, object a_Open, object a_High, object a_Low)
        {
            int rowID = FindRowID(a_OpCode);
            string sideCode = a_OpCode.ToString().Substring(0, 1);

            gridDDE[2, 5].Value = m_OpDataSet.DownLoader.OptTodayTryCount;
            gridDDE[2, 7].Value = m_OpDataSet.DownLoader.OptTodayTryCount;

            string curVal = "";

            if (sideCode == "2")
            {
                curVal = a_Open.ToString() + " : " + a_CurVal.ToString();
                gridDDE[rowID, 4].Value = a_High;
                gridDDE[rowID + 1, 4].Value = curVal;
                gridDDE[rowID + 2, 4].Value = a_Low;
            }
            else if (sideCode == "3")
            {
                curVal = a_Open.ToString() + " : " + a_CurVal.ToString();
                gridDDE[rowID, 8].Value = a_High;
                gridDDE[rowID + 1, 8].Value = curVal;
                gridDDE[rowID + 2, 8].Value = a_Low;
            }

            EasyTrader.Option.DDEDayData curDayCol = m_OpDataSet.FindDDEDayData(m_OpDataSet.DownLoader.CurDay);
            if (CurDayCol != null)
            {
                EasyTrader.Option.DDEData curRowData = null;
                EasyTrader.Option.DDEDataItem curItem = null;
                DDECollectionList dataList = curDayCol.CollectionList;
                for (int i = 0; i < dataList.Count; i++)
                {
                    curRowData = dataList[i];
                    curItem = curRowData.DataItem;
                    if (curRowData.CallCode.ToString() == a_OpCode.ToString())
                    {
                        gridDDE[rowID + 1, 1].Value = "";
                        gridDDE[rowID, 1].Value = "";
                        gridDDE[rowID + 2, 1].Value = "";

                        curItem.CallCurHigh = a_High;
                        curItem.CallCurLow = a_Low;
                        curItem.CallCurOpen = a_Open;
                        curItem.CallCurClose = a_CurVal;

                        CheckCallBase(curRowData, curItem, 1);
                        CheckCallMonthHigh(curRowData, curItem, 1);
                        CheckCallMonthLow(curRowData, curItem, 1);

                    }
                    else if (curRowData.PutCode.ToString() == a_OpCode.ToString())
                    {
                        gridDDE[rowID + 1, 11].Value = "";
                        gridDDE[rowID, 11].Value = "";
                        gridDDE[rowID + 2, 11].Value = "";

                        curItem.PutCurHigh = a_High;
                        curItem.PutCurLow = a_Low;
                        curItem.PutCurOpen = a_Open;
                        curItem.PutCurClose = a_CurVal;

                        CheckPutBase(curRowData, curItem, 11);
                        CheckPutMonthHigh(curRowData, curItem, 11);
                        CheckPutMonthLow(curRowData, curItem, 11);
                    }
                }
            }
        }

        public void SetATM()
        {
            string callATM = m_OpDataSet.DownLoader.CallATM;
            if (callATM != "")
            {
                int curRow = 0;
                string atm = callATM.Substring(callATM.Length - 3, 3);
                for (int i = 2; i < gridDDE.RowsCount; i = i + 3)
                {
                    string pVal = gridDDE[i, 6].Value.ToString();
                    if (atm == pVal)
                    {
                        curRow = i;
                        break;
                    }
                }

                if (curRow != 0)
                {
                    gridDDE[curRow, 6].View = priceView;
                }
            }
        }

        public void SetDayCol(EasyTrader.Option.DDEDayData a_DayCol)
        {
            m_CurDayCol = a_DayCol;
        }

        private void dtPicker_ValueChanged(object sender, EventArgs e)
        {
            DateTime curTime = dtPicker.Value;
            m_WorkDate = GlobalVar.WorkDate = GlobalVar.DateToInt(curTime);
            if (m_WorkDate > GlobalVar.DateToInt())
                m_WorkDate = GlobalVar.DateToInt();

            if (m_WorkDate < Convert.ToInt32(m_OpDataSet.DownLoader.CurBaseDay))
                m_WorkDate = Convert.ToInt32(m_OpDataSet.DownLoader.CurBaseDay);
            EasyTrader.Option.DDEDayData curDayCol = m_OpDataSet.FindDDEDayData(m_WorkDate);
            m_OpDataSet.DownLoader.SetDay(curDayCol.Date);
            FillMonthData(curDayCol);
        }

        public void FillMonthData(DDEDayData dayDataCol)
        {
            if (dayDataCol == null)
                return;

            DDECollectionList dataList = dayDataCol.CollectionList;
            for (int i = 0; i < dataList.Count; i++)
            {
                DDEData curRowData = dataList[i];
                SetBaseDayData(curRowData.CallCode, curRowData.DataItem.CallBase);
                SetBaseDayData(curRowData.PutCode, curRowData.DataItem.PutBase);
                SetMonthData(curRowData.CallCode, curRowData.DataItem.CallMHigh, curRowData.DataItem.CallMLow);
                SetMonthData(curRowData.PutCode, curRowData.DataItem.PutMHigh, curRowData.DataItem.PutMLow);
                SetDailyData(curRowData.CallCode, curRowData.DataItem.CallCurClose, curRowData.DataItem.CallCurOpen, curRowData.DataItem.CallCurHigh, curRowData.DataItem.CallCurLow, curRowData.DataItem.CallPreHigh, curRowData.DataItem.CallPreLow, curRowData.DataItem.CallPreClose, curRowData.DataItem.CallPreOpen);
                SetDailyData(curRowData.PutCode, curRowData.DataItem.PutCurClose, curRowData.DataItem.PutCurOpen, curRowData.DataItem.PutCurHigh, curRowData.DataItem.PutCurLow, curRowData.DataItem.PutPreHigh, curRowData.DataItem.PutPreLow, curRowData.DataItem.PutPreClose, curRowData.DataItem.PutPreOpen);
            }
        }

        private void DDETable_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer.Stop();
        }

        private void btnPreDay_Click(object sender, EventArgs e)
        {
            EasyTrader.Option.DDEDayData preDayCol = m_OpDataSet.FindPrevDDEDayData(m_OpDataSet.DownLoader.CurDay);
            m_OpDataSet.DownLoader.SetDay(preDayCol.Date);
            FillMonthData(preDayCol);
            SetCurDayState(preDayCol);
            SetPreDayState(preDayCol);
            tbCurDay.Text = m_OpDataSet.DownLoader.CurDay.ToString();
            tbPreDay.Text = m_OpDataSet.DownLoader.PreDay.ToString();
            ClearCrossVal(preDayCol);
            SetCrossVal(Convert.ToInt32(m_OpDataSet.DownLoader.CurDay));
            ClearATM(preDayCol);
            SetATM();
            gridDDE.Invalidate();
        }

        private void btnNextDay_Click(object sender, EventArgs e)
        {
            EasyTrader.Option.DDEDayData nextDayCol = m_OpDataSet.FindNextDDEDayData(m_OpDataSet.DownLoader.CurDay);
            m_OpDataSet.DownLoader.SetDay(nextDayCol.Date);
            FillMonthData(nextDayCol);
            SetCurDayState(nextDayCol);
            SetPreDayState(nextDayCol);
            tbCurDay.Text = m_OpDataSet.DownLoader.CurDay.ToString();
            tbPreDay.Text = m_OpDataSet.DownLoader.PreDay.ToString();
            ClearCrossVal(nextDayCol);
            SetCrossVal(Convert.ToInt32(m_OpDataSet.DownLoader.CurDay));
            ClearATM(nextDayCol);
            SetATM();
            gridDDE.Invalidate();
        }

        private void ClearCurDayState(EasyTrader.Option.DDEDayData curDayCol)
        {
            if (curDayCol == null)
                return;

            DDECollectionList dataList = curDayCol.CollectionList;
            for (int i = 0; i < dataList.Count; i++)
            {
                EasyTrader.Option.DDEData curRowData = dataList[i];
                //Console.WriteLine(curRowData.PriceCode);
                EasyTrader.Option.DDEDataItem curItem = curRowData.DataItem;
                int rowID = FindRowID(curRowData.CallCode);
                //gridDDE[rowID + 1, 0].Value = "";
                gridDDE[rowID + 1, 1].Value = "";
                gridDDE[rowID + 1, 11].Value = "";
                //gridDDE[rowID + 1, 12].Value = "";
                //gridDDE[rowID, 0].Value = "";
                gridDDE[rowID, 1].Value = "";
                gridDDE[rowID, 11].Value = "";
                //gridDDE[rowID, 12].Value = "";
                //gridDDE[rowID + 2, 0].Value = "";
                gridDDE[rowID + 2, 1].Value = "";
                gridDDE[rowID + 2, 11].Value = "";
                //gridDDE[rowID + 2, 12].Value = "";
            }
        }

        private void ClearPreDayState(EasyTrader.Option.DDEDayData curDayCol)
        {
            if (curDayCol == null)
                return;

            DDECollectionList dataList = curDayCol.CollectionList;
            for (int i = 0; i < dataList.Count; i++)
            {
                EasyTrader.Option.DDEData curRowData = dataList[i];
                //Console.WriteLine(curRowData.PriceCode);
                EasyTrader.Option.DDEDataItem curItem = curRowData.DataItem;
                int rowID = FindRowID(curRowData.CallCode);
                gridDDE[rowID + 1, 0].Value = "";
                gridDDE[rowID + 1, 12].Value = "";
                gridDDE[rowID, 0].Value = "";
                gridDDE[rowID, 12].Value = "";
                gridDDE[rowID + 2, 0].Value = "";
                gridDDE[rowID + 2, 12].Value = "";
            }
        }

        private void CheckCallBase(EasyTrader.Option.DDEData curRowData, EasyTrader.Option.DDEDataItem curItem, int a_ColNum)
        {
            double callPreClose = Convert.ToDouble(curItem.CallPreClose.ToString());
            double putBase = Convert.ToDouble(curItem.PutBase.ToString());
            double callCurHigh = Convert.ToDouble(curItem.CallCurHigh.ToString());
            double callCurLow = Convert.ToDouble(curItem.CallCurLow.ToString());
            double callCurClose = Convert.ToDouble(curItem.CallCurClose.ToString());
            if (callPreClose < putBase)
            {
                if (callCurHigh > putBase)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (callCurClose >= putBase)
                    {
                        curItem.CallBaseState = 2;
                        gridDDE[rowID + 1, a_ColNum].Value = "▲(기준)";
                        gridDDE[rowID + 1, a_ColNum].View = upView;
                    }
                    else
                    {
                        curItem.CallBaseState = -1;
                        gridDDE[rowID + 1, a_ColNum].Value = "▲▼(기준)";
                        gridDDE[rowID + 1, a_ColNum].View = downView;
                    }
                }
            }
            if (callPreClose > putBase)
            {
                if (callCurLow < putBase)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (callCurClose <= putBase)
                    {
                        curItem.CallBaseState = -2;
                        gridDDE[rowID + 1, a_ColNum].Value = "▼(기준)";
                        gridDDE[rowID + 1, a_ColNum].View = downView;
                    }
                    else
                    {
                        curItem.CallBaseState = 1;
                        gridDDE[rowID + 1, a_ColNum].Value = "▼▲(기준)";
                        gridDDE[rowID + 1, a_ColNum].View = upView;
                    }
                }
            }
        }

        private void CheckCallPreDay(EasyTrader.Option.DDEData curRowData, EasyTrader.Option.DDEDataItem curItem)
        {
            double callPreHigh = Convert.ToDouble(curItem.CallPreHigh.ToString());
            double callPreLow = Convert.ToDouble(curItem.CallPreLow.ToString());
            double callCurClose = Convert.ToDouble(curItem.CallCurClose.ToString());
            if (callCurClose > callPreHigh)
            {
                curItem.CallBaseState = 1;
                int rowID = FindRowID(curRowData.CallCode);
                gridDDE[rowID, 0].Value = "▲(전고)";
                gridDDE[rowID, 0].View = upView;
            }
            if (callCurClose < callPreLow)
            {
                curItem.CallBaseState = -1;
                int rowID = FindRowID(curRowData.CallCode);
                gridDDE[rowID + 2, 0].Value = "▼(전저)";
                gridDDE[rowID + 2, 0].View = downView;
            }
        }

        private void CheckPutPreDay(EasyTrader.Option.DDEData curRowData, EasyTrader.Option.DDEDataItem curItem)
        {
            double putPreHigh = Convert.ToDouble(curItem.PutPreHigh.ToString());
            double putPreLow = Convert.ToDouble(curItem.PutPreLow.ToString());
            double putCurClose = Convert.ToDouble(curItem.PutCurClose.ToString());
            if (putCurClose > putPreHigh)
            {
                curItem.CallBaseState = 1;
                int rowID = FindRowID(curRowData.CallCode);
                gridDDE[rowID, 12].Value = "▲(전고)";
                gridDDE[rowID, 12].View = upView;
            }
            if (putCurClose < putPreLow)
            {
                curItem.CallBaseState = -1;
                int rowID = FindRowID(curRowData.CallCode);
                gridDDE[rowID + 2, 12].Value = "▼(전저)";
                gridDDE[rowID + 2, 12].View = downView;
            }
        }

        private void CheckPutBase(EasyTrader.Option.DDEData curRowData, EasyTrader.Option.DDEDataItem curItem, int a_ColNum)
        {
            double putPreClose = Convert.ToDouble(curItem.PutPreClose.ToString());
            double callBase = Convert.ToDouble(curItem.CallBase.ToString());
            double putCurHigh = Convert.ToDouble(curItem.PutCurHigh.ToString());
            double putCurLow = Convert.ToDouble(curItem.PutCurLow.ToString());
            double putClose = Convert.ToDouble(curItem.PutCurClose.ToString());
            if (putPreClose < callBase)
            {
                if (putCurHigh > callBase)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (putClose >= callBase)
                    {
                        curItem.CallBaseState = 2;
                        gridDDE[rowID + 1, a_ColNum].Value = "▲(기준)";
                        gridDDE[rowID + 1, a_ColNum].View = upView;
                    }
                    else
                    {
                        curItem.CallBaseState = -1;
                        gridDDE[rowID + 1, a_ColNum].Value = "▲▼(기준)";
                        gridDDE[rowID + 1, a_ColNum].View = downView;
                    }
                }
            }
            if (putPreClose > callBase)
            {
                if (putCurLow < callBase)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (putClose <= callBase)
                    {
                        curItem.CallBaseState = -2;
                        gridDDE[rowID + 1, a_ColNum].Value = "▼(기준)";
                        gridDDE[rowID + 1, a_ColNum].View = downView;
                    }
                    else
                    {
                        curItem.CallBaseState = 1;
                        gridDDE[rowID + 1, a_ColNum].Value = "▼▲(기준)";
                        gridDDE[rowID + 1, a_ColNum].View = upView;
                    }
                }
            }
        }

        private void CheckCallMonthHigh(EasyTrader.Option.DDEData curRowData, EasyTrader.Option.DDEDataItem curItem, int a_ColNum)
        {
            double callPreClose = Convert.ToDouble(curItem.CallPreClose.ToString());
            double putMonthHigh = Convert.ToDouble(curItem.PutMHigh.ToString());
            double callCurHigh = Convert.ToDouble(curItem.CallCurHigh.ToString());
            double callCurLow = Convert.ToDouble(curItem.CallCurLow.ToString());
            double callCurClose = Convert.ToDouble(curItem.CallCurClose.ToString());
            if (callPreClose < putMonthHigh)
            {
                if (callCurHigh > putMonthHigh)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (callCurClose >= putMonthHigh)
                    {
                        curItem.CallMonthHState = 2;
                        gridDDE[rowID, a_ColNum].Value = "▲(월고)";
                        gridDDE[rowID, a_ColNum].View = upView;
                    }
                    else
                    {
                        curItem.CallMonthHState = -1;
                        gridDDE[rowID, a_ColNum].Value = "▲▼(월고)";
                        gridDDE[rowID, a_ColNum].View = downView;
                    }
                }
            }
            if (callPreClose > putMonthHigh)
            {
                if (callCurLow < putMonthHigh)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (callCurClose <= putMonthHigh)
                    {
                        curItem.CallMonthHState = -2;
                        gridDDE[rowID, a_ColNum].Value = "▼(월고)";
                        gridDDE[rowID, a_ColNum].View = downView;
                    }
                    else
                    {
                        curItem.CallMonthHState = 1;
                        gridDDE[rowID, a_ColNum].Value = "▼▲(월고)";
                        gridDDE[rowID, a_ColNum].View = upView;
                    }
                }
            }
        }

        private void CheckCallMonthLow(EasyTrader.Option.DDEData curRowData, EasyTrader.Option.DDEDataItem curItem, int a_ColNum)
        {
            double callPreClose = Convert.ToDouble(curItem.CallPreClose.ToString());
            double putMonthLow = Convert.ToDouble(curItem.PutMLow.ToString());
            double callCurHigh = Convert.ToDouble(curItem.CallCurHigh.ToString());
            double callCurLow = Convert.ToDouble(curItem.CallCurLow.ToString());
            double callCurClose = Convert.ToDouble(curItem.CallCurClose.ToString());
            if (callPreClose < putMonthLow)
            {
                if (callCurHigh > putMonthLow)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (callCurClose >= putMonthLow)
                    {
                        curItem.CallMonthLState = 2;
                        gridDDE[rowID + 2, a_ColNum].Value = "▲(월저)";
                        gridDDE[rowID + 2, a_ColNum].View = upView;
                    }
                    else
                    {
                        curItem.CallMonthLState = -1;
                        gridDDE[rowID + 2, a_ColNum].Value = "▲▼(월저)";
                        gridDDE[rowID + 2, a_ColNum].View = downView;
                    }
                }
            }
            if (callPreClose > putMonthLow)
            {
                if (callCurLow < putMonthLow)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (callCurClose <= putMonthLow)
                    {
                        curItem.CallMonthLState = -2;
                        gridDDE[rowID + 2, a_ColNum].Value = "▼(월저)";
                        gridDDE[rowID + 2, a_ColNum].View = downView;
                    }
                    else
                    {
                        curItem.CallMonthLState = 1;
                        gridDDE[rowID + 2, a_ColNum].Value = "▼▲(월저)";
                        gridDDE[rowID + 2, a_ColNum].View = upView;
                    }
                }
            }
        }

        private void CheckPutMonthHigh(EasyTrader.Option.DDEData curRowData, EasyTrader.Option.DDEDataItem curItem, int a_ColNum)
        {
            double putPreClose = Convert.ToDouble(curItem.PutPreClose.ToString());
            double callMonthHigh = Convert.ToDouble(curItem.CallMHigh.ToString());
            double putCurHigh = Convert.ToDouble(curItem.PutCurHigh.ToString());
            double putCurLow = Convert.ToDouble(curItem.PutCurLow.ToString());
            double putClose = Convert.ToDouble(curItem.PutCurClose.ToString());
            if (putPreClose < callMonthHigh)
            {
                if (putCurHigh > callMonthHigh)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (putClose >= callMonthHigh)
                    {
                        curItem.PutMonthHState = 2;
                        gridDDE[rowID, a_ColNum].Value = "▲(월고)";
                        gridDDE[rowID, a_ColNum].View = upView;
                    }
                    else
                    {
                        curItem.PutMonthHState = -1;
                        gridDDE[rowID, a_ColNum].Value = "▲▼(월고)";
                        gridDDE[rowID, a_ColNum].View = downView;
                    }
                }
            }
            if (putPreClose > callMonthHigh)
            {
                if (putCurLow < callMonthHigh)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (putClose <= callMonthHigh)
                    {
                        curItem.PutMonthHState = -2;
                        gridDDE[rowID, a_ColNum].Value = "▼(월고)";
                        gridDDE[rowID, a_ColNum].View = downView;
                    }
                    else
                    {
                        curItem.PutMonthHState = 1;
                        gridDDE[rowID, a_ColNum].Value = "▼▲(월고)";
                        gridDDE[rowID, a_ColNum].View = upView;
                    }
                }
            }
        }

        private void CheckPutMonthLow(EasyTrader.Option.DDEData curRowData, EasyTrader.Option.DDEDataItem curItem, int a_ColNum)
        {
            double putPreClose = Convert.ToDouble(curItem.PutPreClose.ToString());
            double callMonthLow = Convert.ToDouble(curItem.CallMLow.ToString());
            double putCurHigh = Convert.ToDouble(curItem.PutCurHigh.ToString());
            double putCurLow = Convert.ToDouble(curItem.PutCurLow.ToString());
            double putClose = Convert.ToDouble(curItem.PutCurClose.ToString()); ;
            if (putPreClose < callMonthLow)
            {
                if (putCurHigh > callMonthLow)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (putClose >= callMonthLow)
                    {
                        curItem.PutMonthLState = 2;
                        gridDDE[rowID + 2, a_ColNum].Value = "▲(월저)";
                        gridDDE[rowID + 2, a_ColNum].View = upView;
                    }
                    else
                    {
                        curItem.PutMonthLState = -1;
                        gridDDE[rowID + 2, a_ColNum].Value = "▲▼(월저)";
                        gridDDE[rowID + 2, a_ColNum].View = downView;
                    }
                }
            }
            if (putPreClose > callMonthLow)
            {
                if (putCurLow < callMonthLow)
                {
                    int rowID = FindRowID(curRowData.CallCode);
                    if (putClose <= callMonthLow)
                    {
                        curItem.PutMonthLState = -2;
                        gridDDE[rowID + 2, a_ColNum].Value = "▼(월저)";
                        gridDDE[rowID + 2, a_ColNum].View = downView;
                    }
                    else
                    {
                        curItem.PutMonthLState = 1;
                        gridDDE[rowID + 2, a_ColNum].Value = "▼▲(월저)";
                        gridDDE[rowID + 2, a_ColNum].View = upView;
                    }
                }
            }
        }

        public void SetPreDayState(EasyTrader.Option.DDEDayData curDayCol)
        {
            int remDays = m_OpDataSet.DownLoader.GetElapDays();
            if (remDays == 0)
                return;
            EasyTrader.Option.DDEDayData preDayCol = m_OpDataSet.FindPrevDDEDayData(m_OpDataSet.DownLoader.CurDay);

            if (preDayCol == null)
                return;
            ClearPreDayState(preDayCol);

            DDECollectionList dataList = preDayCol.CollectionList;
            for (int i = 0; i < dataList.Count; i++)
            {
                EasyTrader.Option.DDEData curRowData = dataList[i];
                //Console.WriteLine(curRowData.PriceCode);
                EasyTrader.Option.DDEDataItem curItem = curRowData.DataItem;
                CheckCallBase(curRowData, curItem, 0);
                CheckPutBase(curRowData, curItem, 12);
                CheckCallMonthHigh(curRowData, curItem, 0);
                CheckCallMonthLow(curRowData, curItem, 0);
                CheckPutMonthHigh(curRowData, curItem, 12);
                CheckPutMonthLow(curRowData, curItem, 12);
            }
        }

        public void SetCurDayState(EasyTrader.Option.DDEDayData curDayCol)
        {
            if (curDayCol == null)
                return;
            ClearCurDayState(curDayCol);

            DDECollectionList dataList = curDayCol.CollectionList;
            for (int i = 0; i < dataList.Count; i++)
            {
                EasyTrader.Option.DDEData curRowData = dataList[i];
                //Console.WriteLine(curRowData.PriceCode);
                EasyTrader.Option.DDEDataItem curItem = curRowData.DataItem;
                CheckCallBase(curRowData, curItem, 1);
                CheckPutBase(curRowData, curItem, 11);
                CheckCallMonthHigh(curRowData, curItem, 1);
                CheckCallMonthLow(curRowData, curItem, 1);
                CheckPutMonthHigh(curRowData, curItem, 11);
                CheckPutMonthLow(curRowData, curItem, 11);
            }
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            EasyTrader.Option.DDEDayData curDayCol = m_OpDataSet.FindDDEDayData(m_OpDataSet.DownLoader.CurDay);
            if (CurDayCol == null)
                return;
            SetCurDayState(curDayCol);
            SetPreDayState(curDayCol);
        }

        private void btnChart_Click(object sender, EventArgs e)
        {
            //System.Windows.Forms.Form frmchild = new EasyTrader.Chart.CompChart(m_OpDataSet);
            //frmchild.Show();
        }

        private void DDETable_Load(object sender, EventArgs e)
        {
            timer.Start();
        }
    }
}
